import 'package:demo/event/event-bus.dart';
import 'package:demo/event/event-model.dart';
import 'package:demo/pages/activity_page.dart';
import 'package:demo/pages/book_page.dart';
import 'package:demo/pages/index_page.dart';
import 'package:demo/pages/pins_page.dart';
import 'package:demo/pages/repos_page.dart';
import 'package:demo/widgets/login-button.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

class TabNavigator extends StatefulWidget {
  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  int _currentIndex = 0;
  final PageController _controller = PageController(initialPage: 0);

  String _username = '';
  String _userPic = '';

  _TabNavigatorState() {
    final eventBus = new EventBus();
    ApplicationEvent.event = eventBus;
  }

  @override
  void initState() {
    super.initState();
    ApplicationEvent.event.on<UserLoginEvent>().listen((event) {
      print("username = ${event.userName} , userPic = ${event.userPic}");
      setState(() {
        _username = event.userName;
        _userPic = event.userPic;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Image.asset('images/logo.png'),
        backgroundColor: Colors.white,
        title: Text('Flutter 版 web 掘金'),
        actions: <Widget>[
          LoginButton(
            userName: _username,
            userPic: _userPic,
          )
        ],
      ),
      body: PageView(
        controller: _controller,
        children: <Widget>[
          IndexPage(),
          PinsPage(),
          BookPage(),
          ReposPage(),
          ActivityPage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index){
            _controller.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.home, color: _defaultColor),
                activeIcon: Icon(Icons.home, color: _activeColor),
                title: Text('首页', style: TextStyle(color: _currentIndex != 0 ? _defaultColor : _activeColor))
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.chat, color: _defaultColor),
                activeIcon: Icon(Icons.chat, color: _activeColor),
                title: Text('沸点', style: TextStyle(color: _currentIndex != 1 ? _defaultColor : _activeColor))
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.book, color: _defaultColor),
                activeIcon: Icon(Icons.book, color: _activeColor),
                title: Text('小册', style: TextStyle(color: _currentIndex != 2 ? _defaultColor : _activeColor))
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.bubble_chart, color: _defaultColor),
                activeIcon: Icon(Icons.bubble_chart, color: _activeColor),
                title: Text('开源库', style: TextStyle(color: _currentIndex != 3 ? _defaultColor : _activeColor))
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.local_activity, color: _defaultColor),
                activeIcon: Icon(Icons.local_activity, color: _activeColor),
                title: Text('活动', style: TextStyle(color: _currentIndex != 4 ? _defaultColor : _activeColor))
            ),
          ]
      ),
    );
  }
}
